package com.zsrt.day20170425.homework.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.zsrt.day20170425.homework.dao.StudentCRUDDao;
import com.zsrt.day20170425.homework.entity.Student;

/**
*@author jzh
*@创建时间 2017年4月25日下午6:31:44
*类说明：
*/
public class StudentCRUDDaoImpl implements StudentCRUDDao {
	
	
	File file=new File("src/com/zsrt/day20170425/homework/test1.xml");
	/**
	 * 获取Document对象
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public Document getDocument(File file){
		SAXReader saxReader=new SAXReader();
		Document document=null;
		try {
			document = saxReader.read(file);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return document;
	}
	
	@Override
	public int addStudent(File file, Student student) throws Exception {
		
		Document document = getDocument(file);
		Element root = document.getRootElement();
		Element newStudent = root.addElement("student");
		Element addAttribute = newStudent.addAttribute("id", student.getId());
		//Element id = newStudent.addElement("id");student
		Element name = newStudent.addElement("name");
		Element sex = newStudent.addElement("sex");
		Element age = newStudent.addElement("age");
		//id.setText(student.getId());
		name.setText(student.getName());
		sex.setText(student.getSex());
		age.setText(student.getAge().toString());
		WriteToXML(document);
		System.out.println("增加成功");
		return 1;
	}
	
	public void WriteToXML(Document document){
		OutputFormat format=OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter writer=null;
		try {
			OutputStream os=new FileOutputStream(file);
			OutputStreamWriter osw=new OutputStreamWriter(os,"UTF-8");
			writer = new XMLWriter(osw,format);
			writer.write(document);
			writer.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(writer!=null){
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}

	@Override
	public int deleteStudents(File file, String idString) {
		String[] strs=idString.split("[,]");
		for(int i=0;i<strs.length;i++){
			String id=strs[i];
			deleteStudent(file,id);
		}
		
		return 0;
	}

	@Override
	public int deleteStudent(File file, String id) {
		
		SAXReader saxReader=new SAXReader();
		Document document=null;
		try {
			document = saxReader.read(file);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		Element root = document.getRootElement();
		List<Element> elements = root.elements();
		for(int i=0;i<elements.size();i++){
			Element e = elements.get(i);
			String newId = e.attributeValue("id");
			if(id.equals(newId)){
				root.remove(e);
				System.out.println("删除成功");
				return 1;
			}
		}
		WriteToXML(document);
		return 0;
	}

	@Override
	public int updateAgeById(File file, String id,Integer age) {
		
		Document document = getDocument(file);
		Element root = document.getRootElement();
		List<Element> elements = root.elements();
		for(int i=0;i<elements.size();i++){
			Element e = elements.get(i);
			String newId = e.attributeValue("id");
			if(id.equals(newId)){
				Element e1 = e.element("age");
				e1.setText("44");
				WriteToXML(document);
				break;
			}
		}
		
		
		
		return 0;
	}


	/*@Override
	public int deleteStudent(File file, String id) {
		SAXReader saxReader=new SAXReader();
		Document document=null;
		try {
			document = saxReader.read(file);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		Element root = document.getRootElement();
		List<Element> elements = root.elements();
		for(int i=0;i<elements.size();i++){
			Element e = elements.get(i);
			List<Element> childElements = e.elements();
			for(int j=0;j<childElements.size();j++){
				//String newId = childElements.get(j).getStringValue();
				String newId = childElements.get(j).getText();
				if(id.equals(newId)){
					root.remove(e);
					WriteToXML(document);
					System.out.println("删除成功");
					return 1;
				}
			}
			
		}
		
		return 0;
	}
*/
	
	
}
